package com.service.admin.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.service.admin.base.controller.req.RecommendAwardDeleteReq;
import com.service.admin.base.service.RecommendAwardConfigService;
import com.service.admin.support.ResultCodeEnum;
import com.service.core.constant.BusinessStatus;
import com.service.core.exception.CommonException;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.MultiResponse;
import com.service.core.rest.response.OneResponse;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.model.RecommendAwardConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;


/**
 * 间接奖考核条件以及奖金比例
 *
 * @author xxxx
 * @email xxxx@qq.com
 * @date 2020-01-02 16:37:27
 */
@RestController
@RequestMapping("config/recommend-award")
public class RecommendAwardConfigController extends BaseController {
    @Autowired
    private RecommendAwardConfigService recommendAwardConfigService;

    /**
     * 间接奖考核条件以及奖金比例
     * 列表
     *
     * @author xxxx
     * @email xxxx@qq.com
     * @date 2020-01-02 16:37:27
     */
    @GetMapping("/list-all")
    public MultiResponse<RecommendAwardConfig> listAll() {
        return successQueryMultiResponse(recommendAwardConfigService.list());
    }

    /**
     * 间接奖考核条件以及奖金比例
     * 信息
     *
     * @author xxxx
     * @email xxxx@qq.com
     * @date 2020-01-02 16:37:27
     */
    @GetMapping("/load/{id}")
    public OneResponse<RecommendAwardConfig> load(@PathVariable("id") Integer id) {
        return successQueryOneResponse(recommendAwardConfigService.getById(id));
    }

    /**
     * 间接奖考核条件以及奖金比例
     * 保存
     *
     * @author xxxx
     * @email xxxx@qq.com
     * @date 2020-01-02 16:37:27
     */
//    @PostMapping("/create")
    public BaseResponse create(@RequestBody RecommendAwardConfig recommendAwardConfig) {
        recommendAwardConfigService.save(recommendAwardConfig);
        return successSaveBaseResponse();
    }

    /**
     * 间接奖考核条件以及奖金比例
     * 修改
     *
     * @author xxxx
     * @email xxxx@qq.com
     * @date 2020-01-02 16:37:27
     */
    @PostMapping("/modify")
    public BaseResponse modify(@RequestBody RecommendAwardConfig recommendAwardConfig) {
        recommendAwardConfigService.updateById(recommendAwardConfig);
        return successUpdateBaseResponse();
    }



    @PostMapping("/update-status/{id}")
    public BaseResponse updateStatus(@PathVariable("id") Integer id) {
        RecommendAwardConfig config = recommendAwardConfigService.getById(id);
        if(ObjectUtils.isEmpty(config)){
            throw new CommonException(ResultCodeEnum.CONFIG_NOT_EXIST.getValue(), ResultCodeEnum.CONFIG_NOT_EXIST.getReasonPhrase());
        }
        Integer configStatus = ObjectUtils.nullSafeEquals(CommonStatusEnums.ACTIVE.getCode(), config.getConfigStatus())
                ? CommonStatusEnums.LOCK.getCode() : CommonStatusEnums.ACTIVE.getCode();
        recommendAwardConfigService.updateById(RecommendAwardConfig.builder()
                .id(id).configStatus(configStatus).build());
        return successDeleteBaseResponse();
    }



}
